package org.linlinjava.litemall.db.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.dao.LumiereSubscribeMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class LumiereSubscribeService extends MPJBaseServiceImpl<LumiereSubscribeMapper,LumiereSubscribe> {
    @Resource
    private LumiereSubscribeMapper lumiereSubscribeMapper;

    public IPage<LumiereSubscribe> querySelective(String name, Integer page,Integer status,Byte useRange, Integer limit, String sort, String order) {
       return lumiereSubscribeMapper.selectPage(new Page(page,limit), Wrappers.lambdaQuery(LumiereSubscribe.class)
                .like(!StringUtils.isEmpty(name),LumiereSubscribe::getName,name)
                .eq(status != null,LumiereSubscribe::getStatus,status)
                .eq(useRange != null,LumiereSubscribe::getUseRange,useRange)
                .eq(LumiereSubscribe::getDeleted,0)
                .orderByDesc(LumiereSubscribe::getAddTime));
    }

    public int add(LumiereSubscribe lumiereSubscribe){
       return  lumiereSubscribeMapper.insertSelective(lumiereSubscribe);
    }

    public LumiereSubscribe querySubscribeById(Integer suscribeId){
       return lumiereSubscribeMapper.selectByPrimaryKey(suscribeId);
    }

    public int update(LumiereSubscribe lumiereSubscribe){
        return lumiereSubscribeMapper.updateByPrimaryKeySelective(lumiereSubscribe);
    }

    public void deleteById(Integer id) {
        lumiereSubscribeMapper.logicalDeleteByPrimaryKey(id);
    }

    public LumiereSubscribe findById(Integer id) {
        return lumiereSubscribeMapper.selectByPrimaryKey(id);
    }

    public IPage<LumiereSubscribe> queryAll(Integer page, Integer limit, String sort, String order, Integer subscribeId){
        LambdaQueryWrapper<LumiereSubscribe> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(subscribeId != null,LumiereSubscribe::getId,subscribeId);
        lambdaQueryWrapper.eq(LumiereSubscribe::getDeleted,false);
        lambdaQueryWrapper.orderByDesc(LumiereSubscribe::getAddTime);
        return lumiereSubscribeMapper.selectPage(new Page<>(page,limit),lambdaQueryWrapper);
    }

    public List<LumiereSubscribe> queryAllPushed(){
        LumiereSubscribeExample example = new LumiereSubscribeExample();
        LumiereSubscribeExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.SUBSCRIBE_STATUS_INUSE).andDeletedEqualTo(false);

        return lumiereSubscribeMapper.selectByExample(example);
    }


}
